package edu.dublbo.app.generator.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具集
 *
 * @author habitplus
 * @since 2023/3/6 7:31
 */
public class StrUtils {
    public static final String EMPTY_STR = "";

    private static final Pattern PATTERN = Pattern.compile("-?[0-9]+(\\\\.[0-9]+)?");

    public static boolean isBlank(String s) {
        return isEmpty(s) || EMPTY_STR.equals(s.trim());
    }

    public static boolean isEmpty(String s) {
        return s == null || s.length() < 1;
    }

    public static boolean isNumber(String str) {
        // 通过Matcher进行字符串匹配
        Matcher m = PATTERN.matcher(str);
        // 如果正则匹配通过 m.matches() 方法返回 true ，反之 false
        return m.matches();
    }


    /**
     * 不能为空，只能含有字母，数字，下划线，且不能以数字或下划线开头
     */
    public static boolean isValidClassName(String name) {
        if (name != null && name.length() > 0 && Character.isLetter(name.charAt(0))) {
            Pattern pattern = Pattern.compile("^[_A-Za-z0-9]+$");
            Matcher matcher = pattern.matcher(name);
            return matcher.matches();
        }
        return false;
    }
}
